home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Peter's Final Project / sys / main.c next >
Text File  |  1995-05-10  |  5KB  |  332 lines

  1. /*
  2.  *  Peter's Final Project -- A texture mapping demonstration
  3.  *  © 1995, Peter Mattis
  4.  *
  5.  *  E-mail:
  6.  *  petm@soda.csua.berkeley.edu
  7.  *
  8.  *  Snail-mail:
  9.  *   Peter Mattis
  10.  *   557 Fort Laramie Dr.
  11.  *   Sunnyvale, CA 94087
  12.  *
  13.  *  Avaible from:
  14.  *  http://www.csua.berkeley.edu/~petm/final.html
  15.  *
  16.  *  This program is free software; you can redistribute it and/or modify
  17.  *  it under the terms of the GNU General Public License as published by
  18.  *  the Free Software Foundation; either version 2 of the License, or
  19.  *  (at your option) any later version.
  20.  *
  21.  *  This program is distributed in the hope that it will be useful,
  22.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  *  GNU General Public License for more details.
  25.  *
  26.  *  You should have received a copy of the GNU General Public License
  27.  *  along with this program; if not, write to the Free Software
  28.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  */
  30.  
  31. #include <console.h>
  32. #include <profile.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36.  
  37. #include "engine.h"
  38. #include "utils.h"
  39. #include "sys.stuff.h"
  40.  
  41. /*
  42.  * Declare the functions private to this module.
  43.  */
  44.  
  45. void do_event_loop (void);
  46. void do_handle_event (long, long);
  47. void do_quit (void);
  48. void do_parse_command_line (int, char **);
  49. short parse_option (char *);
  50. short parse_depth (char *);
  51. short parse_viewsize (char *);
  52. short parse_mazesize (char *);
  53. short parse_wall_texture (char *);
  54. short parse_floor_texture (char *);
  55. short parse_ceiling_texture (char *);
  56. short parse_mode (char *);
  57. short parse_debug (char *);
  58.  
  59. /*
  60.  * Need a variable to keep track of whether
  61.  *  the program is done or not. Also need one
  62.  *  for storing the program name.
  63.  */
  64.  
  65. static short done = 0;
  66. char *prog_name;
  67.  
  68. int
  69. main (argc, argv)
  70.     int argc;
  71.     char *argv[];
  72. {
  73. /*
  74.  * Replace with 1 to turn on profiling.
  75.  */
  76. #if 0
  77.     _ftype = 'TEXT';
  78.     _fcreator = 'R*ch';
  79.     freopen("out", "w+", stdout);
  80.  
  81.     InitProfile(200, 200);
  82.     _profile = 1;
  83.     _trace = 0;
  84. #endif
  85.  
  86.     /*
  87.      * Get a command line.
  88.      */
  89.     argc = ccommand (&argv);
  90.  
  91.     /*
  92.      * Parse the command line options.
  93.      */
  94.     do_parse_command_line (argc, argv);
  95.     
  96.     /*
  97.      * Initialize the system. (Create the window, set the
  98.      *  palette, etc)
  99.      */
  100.     do_sys_init ();
  101.     
  102.     /*
  103.      * Set the event handler.
  104.      */
  105.     do_set_event_handler (do_handle_event);
  106.     
  107.     /*
  108.      * Initialize the engine.
  109.      */
  110.     do_engine_init ();
  111.  
  112.     /*
  113.      * Enter the event loop.
  114.      */
  115.     do_event_loop ();
  116.  
  117.     /*
  118.      * Exit the engine.
  119.      */
  120.     do_engine_exit ();
  121.  
  122.     /*
  123.      * Exit the system.
  124.      */
  125.     do_sys_exit ();
  126.  
  127.     return 0;
  128. }
  129.  
  130. /*
  131.  * The event loop.
  132.  */
  133.  
  134. void
  135. do_event_loop ()
  136. {
  137.     long event, val;
  138.  
  139.     while (!done)
  140.     {
  141.         do_sys_event ();
  142.         do_engine_frame ();
  143.     }
  144. }
  145.  
  146. /*
  147.  * Handle an event.
  148.  */
  149.  
  150. void
  151. do_handle_event (event, val)
  152.     long event, val;
  153. {
  154.     switch (event)
  155.     {
  156.     case KEY_PRESS_EVENT:
  157.         /*
  158.          * Pass all key presses to the engine
  159.          *  except for the ESC key, which quits
  160.          *  the program.
  161.          */
  162.          
  163.         if (val == ESC_KEY)
  164.             do_quit ();
  165.         else
  166.             do_engine_key (val, 1);
  167.         break;
  168.     case KEY_RELEASE_EVENT:
  169.         do_engine_key (val, 0);
  170.         break;
  171.     case BUTTON_PRESS_EVENT:
  172.         break;
  173.     case BUTTON_RELEASE_EVENT:
  174.         break;
  175.     default:
  176.         break;
  177.     }
  178. }
  179.  
  180. void
  181. do_quit ()
  182. {
  183.     done = 1;
  184. }
  185.  
  186. void
  187. do_parse_command_line (argc, argv)
  188.     int argc;
  189.     char *argv[];
  190. {
  191.     short i;
  192.     char *token;
  193.  
  194.     prog_name = argv[0];
  195.  
  196.     for (i = 1; i < argc; i++)
  197.     {
  198.         parse_option (argv[i]);
  199.     }
  200. }
  201.  
  202. short
  203. parse_option (token)
  204.     char *token;
  205. {
  206.     if (token[0] == '-')
  207.     {
  208.         token = &token[1];
  209.         if (parse_depth (token) ||
  210.             parse_viewsize (token) ||
  211.             parse_mazesize (token) ||
  212.             parse_wall_texture (token) ||
  213.             parse_floor_texture (token) ||
  214.             parse_ceiling_texture (token) ||
  215.             parse_mode (token) ||
  216.             parse_debug (token))
  217.             return;
  218.         else
  219.             printf ("unknown option: %s\n", token);
  220.     }
  221.     else
  222.         {
  223.         printf ("unable to parse: %s\n", token);
  224.     }
  225.  
  226.     return 0;
  227. }
  228.  
  229. short
  230. parse_depth (token)
  231.     char *token;
  232. {
  233.     if (token[0] == 'd')
  234.     {
  235.         do_set_depth (atoi (&token[1]));
  236.         return 1;
  237.     }
  238.  
  239.     return 0;
  240. }
  241.  
  242. short
  243. parse_viewsize (token)
  244.     char *token;
  245. {
  246.     if (token[0] == 'v')
  247.     {
  248.         do_set_window_size (atoi (&token[1]));
  249.         return 1;
  250.     }
  251.  
  252.     return 0;
  253. }
  254.  
  255. short
  256. parse_mazesize (token)
  257.     char *token;
  258. {
  259.     if (token[0] == 'm')
  260.     {
  261.         do_set_maze_size (atoi (&token[1]));
  262.         return 1;
  263.     }
  264.  
  265.     return 0;
  266. }
  267.  
  268. short
  269. parse_wall_texture (token)
  270.     char *token;
  271. {
  272.     if (token[0] == 'w')
  273.     {
  274.         do_set_wall_texture (&token[1]);
  275.         return 1;
  276.     }
  277.  
  278.     return 0;
  279. }
  280.  
  281. short
  282. parse_floor_texture (token)
  283.     char *token;
  284. {
  285.     if (token[0] == 'f')
  286.     {
  287.         do_set_floor_texture (&token[1]);
  288.         return 1;
  289.     }
  290.  
  291.     return 0;
  292. }
  293.  
  294. short
  295. parse_ceiling_texture (token)
  296.     char *token;
  297. {
  298.     if (token[0] == 'c')
  299.     {
  300.         do_set_ceiling_texture (&token[1]);
  301.         return 1;
  302.     }
  303.  
  304.     return 0;
  305. }
  306.  
  307. short
  308. parse_mode (token)
  309.     char *token;
  310. {
  311.     if (token[0] == 'M')
  312.     {
  313.         do_set_mode (atoi (&token[1]));
  314.         return 1;
  315.     }
  316.  
  317.     return 0;
  318. }
  319.  
  320. short
  321. parse_debug (token)
  322.     char *token;
  323. {
  324.     if (token[0] == 'D')
  325.     {
  326.         do_set_debug_level (atoi (&token[1]));
  327.         return 1;
  328.     }
  329.  
  330.     return 0;
  331. }
  332.